package top.eggcode.setup.config.session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import top.eggcode.common.cache.RedisUnit;
import top.eggcode.plugins.session.IdGenerator;
import top.eggcode.plugins.session.SessionHelper;
import top.eggcode.plugins.session.SimpleIdGenerator;
import top.eggcode.plugins.session.manager.SessionManager;
import top.eggcode.plugins.session.manager.SimpleSessionManager;

/**
 * Title:
 * Description: TODO
 * Date: 2021/6/12 19:57
 *
 * @author JiaQi Ding
 * @version 1.0
 */
//@Configuration
public class AppSessionConfig {

    @Bean
    public AppCenterSessionStore redisSessionDAO(RedisUnit buffer) {

        IdGenerator idGenerator = new SimpleIdGenerator();
        return new AppCenterSessionStore(buffer, idGenerator);
    }

    @Bean("sessionManager")
    public SessionManager createVersionedSessionManager() {
        return new SimpleSessionManager();
    }

    /**
     * 使用 redis 作为 session 容器
     *
     * @param sessionDAO
     * @return
     */
    @Bean("redisSessionManager")
    public SessionManager setSessionManager(AppCenterSessionStore sessionDAO) {
        SimpleSessionManager manager = new SimpleSessionManager();
        manager.setSessionDAO(sessionDAO);
        // 存活时间 30 分钟
        manager.setGlobalSessionTimeout(1800);
        return manager;
    }

    @Qualifier("redisSessionManager")
    //@Qualifier("sessionManager")
    @Autowired(required = true)
    public void loadSessionManager(SessionManager sessionManager) {
        SessionHelper.setSessionManager(sessionManager);
    }

}
